#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include "ioctl.h"



int strLength(const char *s);

int main(int argc, char **argv)
{
	int fd;
	int ret;
	char sysin[1024];
	char sysout[1024] = {0};
	char *filename = "/dev/test";
	
	if(argc < 2){
		printf("Usage:%s <rev or pos>\n",argv[0]);
		return -1;
	}
	
	/* 打开驱动文件 */
	fd = open(filename,O_RDWR);
	
	
	if(fd<0)
	{
		printf("Can't open file %s\r\n", filename);
		return fd;
	}

	
	printf("请输入字符串：");
	memset(sysin,0,1024);
	gets(sysin);

	write(fd, sysin, strLength(sysin));
	if(strcmp(argv[1], "rev") == 0){
		ret = ioctl(fd,REV_ORDER); 
	}
	if(strcmp(argv[1], "pos") == 0){
		ret = ioctl(fd,POS_ORDER); 
	}
	
	read(fd,sysout,1024); //读数据

	
	printf("read = %s \n",sysout);

	/* 关闭设备 */
	ret = close(fd);
	if(ret < 0){
		printf("Can't close file %s\r\n", filename);
		return ret;
	}
	return 0;
}

int strLength(const char *s)
{
	int len;
	len=0;
	while((*s)!=0)
	{
		s++;
		len++;
	}
	return len;
}
